package com.itheima.article.controller;

import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApAuthor;
import com.itheima.article.service.ApArticleService;
import com.itheima.article.service.ApAuthorService;
import com.itheima.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Cheneg
 * @version 1.0
 */
@RestController
public class ApiController {
    @Autowired
    private ApAuthorService apAuthorService;

    /**
     * 通过用户id与自媒体账号id获取作者信息
     * @param apUserId 用户id
     * @param wmUserId 自媒体账号id
     * @return
     */
    @GetMapping("/api/apAuthor/{apUserId}/{wmUserId}")
    public ResultVo<ApAuthor> getByApUserIdWmUserId(
            @PathVariable(name = "apUserId") Integer apUserId,
            @PathVariable(name = "wmUserId") Integer wmUserId){
        ApAuthor apAuthor = apAuthorService.getByApUserIdWmUserId(apUserId, wmUserId);
        return ResultVo.ok(apAuthor);
    }

    /**
     * 创建作者账号
     * @param author
     * @return
     */
    @PostMapping("/api/apAuthor/add")
    public ResultVo add(@RequestBody ApAuthor author){
        apAuthorService.save(author);
        return ResultVo.ok();
    }

    @Autowired
    private ApArticleService apArticleService;

    /**
     * 同步自媒体文章到文章微服
     * @param dto
     * @return
     */
    @PostMapping("/api/apArticle/saveArticleInfo")
    public ResultVo saveArticleInfo(@RequestBody ArticleInfoDto dto){
        apArticleService.saveArticleInfo(dto);
        return ResultVo.ok();
    }

    /**
     * 通过文章id查询作者名称
     * @param articleId
     * @return
     */
    @GetMapping("/api/apArticle/getAuthorName/{articleId}")
    public ResultVo<String> getAuthorNameByArticleId(@PathVariable(value = "articleId")
                                                             Long articleId){
        ApArticle article = apArticleService.getById(articleId);
        Object authorName = article.getAuthorName();
        return ResultVo.ok(authorName);
    }
}



